package cn.wsalix.observable.service.impl;

import java.util.Observable;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.wsalix.observable.service.BuyService;
import cn.wsalix.order.entity.Order;
import cn.wsalix.order.form.OrderForm;
import cn.wsalix.order.service.OrderInfoService;
import cn.wsalix.order.service.OrderService;
import cn.wsalix.user.service.AddressService;

@Service("buyService")
public class BuyServiceImpl extends Observable implements BuyService {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderInfoService orderInfoService;
	@Autowired
	private AddressService addressService;
	//@Autowired
	//private MyProductService myProductService;
	private Order order;
	@PostConstruct
	public void init() {
		logger.info("==========init");
		//addObserver(myProductService);
		addObserver(addressService);
		addObserver(orderInfoService);
		addObserver(orderService);
	}

	@PreDestroy
	public void destroy() {
		logger.info("==========destroy");
	}
	@Transactional
	@Override
	public Order buy(OrderForm entity) {
		setChanged();
		notifyObservers(entity);
		orderService.edit(order);
		return order;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}
	@Transactional
	@Override
	public Order tuanBuy(OrderForm entity) {
		setChanged();
		notifyObservers(entity);
		orderService.edit(order);
		return order;
	}
	
}
